// pages/me/offerPrice/offerPrice.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        searchParams:{
            pageNum: 1,
            pageSize: 6,
            queryType:'1',
            pages:'',
            total:'',
        },
        noData: true,
        InitData:false,
        applyList:{},
        reportApplyAudit: false
    },

    doNothing(){},
    onLoad: function (options) {
        this.InitUserStatus();
        this.setData({
          'InitData': true
        })
    },
    // 检查一下登录状态。
    async InitUserStatus(){
        app.showLoading("获取用户信息");
        await app.checkToken();
        this.InitUserInfo();
        wx.hideLoading();
    },
    InitUserInfo(){
        let userInfo = app.globalData.userInfo;
        // 这里再验证一遍权限。
        if (userInfo.roleList.indexOf('reportApplyAudit') == -1) {
            this.setData({
                'searchParams.queryType': 2
            })
        }else{
            this.setData({
                'reportApplyAudit': true
            })
        }
        this.goQuery();
    },
    goQuery(){
      this.setData({
        'searchParams.pageNum':1,
        'searchParams.total':'',
        'searchParams.pages':'',
        'applyList': {}
      });
      this.reportApplyList(this.data.searchParams);
    },
    reportApplyList(searchParams){
      // 整理完成进行搜索操作。
      app.sHttp.reportApplyList(searchParams).then(res=>{
          let applyList = this.data.applyList;
          applyList[searchParams.pageNum] = res.result.applyList;
          if (0 == res.result.total) {
            this.setData({
              'noData': true
            })
          }else{
            this.setData({
                'noData': false,
                'applyList': applyList,
                'searchParams.total':res.result.total,
                'searchParams.pages': res.result.pages
            });
          }
        },err=>{
          app.error(err.msg);
        })
    },
    switchType(e){
        let type = e.currentTarget.dataset.type;
        if(!this.data.reportApplyAudit){
            if(1 == type || '1' == type){
                app.alert('您无权查看');
                return;
            }
        }
        let _this = this;
        this.setData({
            'searchParams.queryType': type
        },(res)=>{
            _this.goQuery();
        })
    },
    onReachBottom(){
        if (this.data.searchParams.pageNum == this.data.searchParams.pages) {
            //app.alert('没有更多了哦~');
        }else{
            let self = this;
            this.setData({
                'searchParams.pageNum': self.data.searchParams.pageNum+1
            });
            this.reportApplyList(this.data.searchParams);
        }
    },
    okOrJujue(e){
        let page = e.currentTarget.dataset.page;
        let index = e.currentTarget.dataset.idx;
        let item = this.data.applyList[page][index];
        let status = e.currentTarget.dataset.status;
        wx.showModal({
            content: '您确定要' + (status == 1?'同意':'拒绝') + '他查看报告吗？',
            showCancel: true,
            title: (status == 1?'同意':'拒绝') + '申请',
            success: (result) => {
                if (result.confirm) {
                    app.sHttp.reportApplyAudit({applyId: item.applyId,status:Number(status)}).then(res=>{
                        // 重新加载当前页面
                        this.reportApplyList({
                            pageNum: page,
                            pageSize: 6,
                            queryType:this.data.searchParams.queryType});
                    },err=>{
                        app.error(err.msg);
                    })
                }
            },
            fail: (res) => {}
        })
    },
    makePhoneCall(e){
        let page = e.currentTarget.dataset.page;
        let index = e.currentTarget.dataset.idx;
        let item = this.data.applyList[page][index];
        wx.makePhoneCall({
            phoneNumber: item.applyUserMobile,
            success:function(res){
                console.log(res);
            },
            fail:function(){}
        });
    },
    lookReprotInfo(e){
      app.globalData.lookReportInfoUrl = e.currentTarget.dataset.url;
      wx.navigateTo({
        url: '/pages/carQueryRecord/detailed/detailed',
      })
    },
})